<?php
/**
 * Created by Teacher黄.
 * User: Administrator
 * Date: 2017/10/25
 * Time: 15:54
 */

namespace app\index\controller;


use app\common\controller\Base;
use app\index\model\Admin;
use think\Request;
use think\Session;

class Login extends Base
{
    public function login(Request $request){
        if($request->isGet()){
            return $this->fetch();
        }else{ # 登陆操作
            $admin_name = $request->post('admin_name');
            $admin_pwd = $request->post('admin_pwd');
            # 执行查询操作
            $admin = new Admin();
            $res = $admin->where([
                'admin_name' => $admin_name,
                'admin_pwd' => md5($admin_pwd)
            ])->find();

            if($res){
                #登陆成功
                # 设置session
                $data = $res->toArray();
                Session::set('admin_info',$data);
                Session::set('is_login',1);# 登陆状态
                $this->success('登陆成功!','index/index');
            }else{
                $this->error('登陆失败！');
            }
        }

    }



    # 登出
    public function logout(){
        # 消除session
        Session::delete('is_login');
        Session::delete('admin_info');

        $this->success('退出成功！','login/login');
    }
}